#pragma once

#include <iostream>
#include <functional>
#include <string>
#include "log.hpp"
using namespace std;
void serviceIO(int sock)
{
    char buffer[1024];
    while (true)
    {
        ssize_t n = read(sock, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            // 目前我们把读到的数据当成字符串,截止目前
            buffer[n] = 0;
            cout << "recv message: " << buffer << endl;

            // 将从客户端读到的数据在返回给客户端
            string outbuffer = buffer;
            outbuffer += "server[echo]";

            write(sock, outbuffer.c_str(), outbuffer.size());
        }
        else if (n == 0)
        {
            // 在网络中对于socket，表示连接已关闭
            logMessage(NORMAL, "Clinet quit , me too");
            break;
        }
        
    }
    close(sock);
}

class Task
{
    using func_t = std::function<void(int)>;
    // typedef std::function<int(int,int)> func_t;
public:
    Task()
    {
    }
    Task(int sock, func_t func)
        : _sock(sock), _callback(func)
    {
    }
    void operator()()
    {
        _callback(_sock);
    }

private:
    int _sock;
    func_t _callback;
};


